from django.shortcuts import render, HttpResponse, redirect, reverse
from crm import models

from utils.pagination import Pagination

from crm.views.base import ShowList
from crm.forms import ClassForm, CourseRecordForm, StudyRecordForm


class ClassList(ShowList):

    def get(self, request, *args, **kwargs):
        all_class = models.ClassList.objects.all()

        page = Pagination(request.GET.get('page', 1), all_class.count(), request.GET.copy())

        return render(request, 'teacher/class_list.html',
                      {'all_class': all_class[page.start:page.end], 'page_html': page.page_html})


def class_change(request, pk=None):
    obj = models.ClassList.objects.filter(pk=pk).first()

    form_obj = ClassForm(instance=obj)
    if request.method == 'POST':
        form_obj = ClassForm(request.POST, instance=obj)
        if form_obj.is_valid():
            form_obj.save()
            next = request.GET.get('next')
            if next:
                return redirect(next)
            return redirect('crm:class_list')

    title = '编辑班级' if pk else '添加班级'
    return render(request, 'form.html', {'form_obj': form_obj, 'title': title})


class CourseRecordList(ShowList):

    def get(self, request, class_id, *args, **kwargs):
        all_course_record = models.CourseRecord.objects.filter(re_class_id=class_id)

        page = Pagination(request.GET.get('page', 1), all_course_record.count(), request.GET.copy())

        return render(request, 'teacher/course_record_list.html',
                      {'all_course_record': all_course_record[page.start:page.end], 'page_html': page.page_html,
                       'class_id': class_id})

    def multi_init(self):
        # 批量初始化学习记录

        course_record_list = models.CourseRecord.objects.filter(pk__in=self.request.POST.getlist('pk'))
        for course_record in course_record_list:
            # 学生
            students = course_record.re_class.customer_set.filter(status='studying')

            # for student in students:
            #     models.StudyRecord.objects.create(course_record=course_record, student=student)

            # 批量操作 插入数据
            study_record_list = []

            for student in students:
                if not models.StudyRecord.objects.filter(course_record=course_record, student=student).exists():
                    study_record_list.append(models.StudyRecord(course_record=course_record, student=student))

            models.StudyRecord.objects.bulk_create(study_record_list)  # 批量插入


def course_record_change(request, class_id=None, pk=None):
    obj = models.CourseRecord(re_class_id=class_id,
                              recorder=request.user_obj) if class_id else models.CourseRecord.objects.filter(
        pk=pk).first()

    form_obj = CourseRecordForm(instance=obj)
    if request.method == 'POST':
        form_obj = CourseRecordForm(request.POST, instance=obj)
        if form_obj.is_valid():
            form_obj.save()
            next = request.GET.get('next')

            return redirect(next)

    title = '编辑课程记录' if pk else '添加课程记录'
    return render(request, 'form.html', {'form_obj': form_obj, 'title': title})


from django.forms import modelformset_factory


def study_record(request, course_record_id):
    FormSet = modelformset_factory(model=models.StudyRecord, form=StudyRecordForm, extra=0)

    formset_obj = FormSet(queryset=models.StudyRecord.objects.filter(course_record_id=course_record_id))

    if request.method == 'POST':
        formset_obj = FormSet(data=request.POST)
        if formset_obj.is_valid():
            formset_obj.save()
            next = request.GET.get('next')
            return redirect(next)

    return render(request, 'teacher/study_record.html', {'formset_obj': formset_obj})
